<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref } from "vue";
import {
  AddTeachingWorkloadCommand,
  TeachingWorkloadPageResponse,
  UpdateTeachingWorkloadCommand,
  addTeachingWorkloadApi,
  updateTeachingWorkloadApi
} from "@/api/performance/teaching";
import { ElMessage, FormInstance, FormRules } from "element-plus";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: TeachingWorkloadPageResponse;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const formData = reactive<AddTeachingWorkloadCommand | UpdateTeachingWorkloadCommand>({
  id: 0,
  userId: 0,
  assessmentYear: new Date().getFullYear(),
  generalCourseScore: 0,
  subjectCourseScore: 0,
  labCourseScore: 0,
  totalTeachingScore: 0
});

const rules: FormRules = {
  userId: [
    {
      required: true,
      message: "用户ID不能为空"
    }
  ],
  assessmentYear: [
    {
      required: true,
      message: "考核年度不能为空"
    }
  ]
};

const formRef = ref<FormInstance>();
function handleOpened() {
  if (props.row) {
    Object.assign(formData, props.row);
  } else {
    formRef.value?.resetFields();
  }
}

const loading = ref(false);
async function handleConfirm() {
  try {
    loading.value = true;
    if (props.type === "add") {
      await addTeachingWorkloadApi(formData);
    } else if (props.type === "update") {
      await updateTeachingWorkloadApi(formData as UpdateTeachingWorkloadCommand);
    }
    ElMessage.info("提交成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "提交失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增教学工作量' : '更新教学工作量'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    @opened="handleOpened"
  >
    <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item prop="userId" label="用户ID" required>
        <el-input-number :min="1" v-model="formData.userId" />
      </el-form-item>
      <el-form-item prop="assessmentYear" label="考核年度" required>
        <el-input-number :min="2000" :max="2100" v-model="formData.assessmentYear" />
      </el-form-item>
      <el-form-item prop="generalCourseScore" label="通用课程分数">
        <el-input-number :min="0" v-model="formData.generalCourseScore" />
      </el-form-item>
      <el-form-item prop="subjectCourseScore" label="专业课程分数">
        <el-input-number :min="0" v-model="formData.subjectCourseScore" />
      </el-form-item>
      <el-form-item prop="labCourseScore" label="实验课程分数">
        <el-input-number :min="0" v-model="formData.labCourseScore" />
      </el-form-item>
      <el-form-item prop="totalTeachingScore" label="总教学分数">
        <el-input-number :min="0" v-model="formData.totalTeachingScore" />
      </el-form-item>
    </el-form>
  </v-dialog>
</template>
